package aishua.com.mytestapplist.mgr;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

import aishua.com.mytestapplist.utils.CommonConstants;


/**
 * Created by aishua on 2017/7/5.
 */

public class FileMgr {
    private static final int BUFFER_SIZE = 16384;
    public static final Charset UTF_8 = Charset.forName("UTF-8");
    public static String inputStreamToString(InputStream is) throws Exception {
        try {
            return new String(inputStreamToBa(is), UTF_8);
        } catch (Exception ex) {
           ex.getMessage();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    public static void deleteDirectory(File directory) throws IOException {
        if (directory.exists()) {
            cleanDirectory(directory);
            if (!directory.delete()) {
                throw new IOException("Unable to delete directory " + directory + ".");
            }
        }
    }

    public static void cleanDirectory(File directory) throws IOException {
        if (!directory.exists()) {
            throw new IllegalArgumentException(directory + " does not exist");
        } else if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files == null) {
                throw new IOException("Failed to list contents of " + directory);
            }
            IOException exception = null;
            for (File file : files) {
                try {
                    forceDelete(file);
                } catch (IOException ioe) {
                    exception = ioe;
                }
            }
            if (exception != null) {
                throw exception;
            }
        } else {
            throw new IllegalArgumentException(directory + " is not a directory");
        }
    }

    public static void forceDelete(File file) throws IOException {
        if (file.isDirectory()) {
            deleteDirectory(file);
            return;
        }
        boolean filePresent = file.exists();
        if (!file.delete()) {
            if (filePresent) {
                throw new IOException("Unable to delete file: " + file);
            }
            throw new FileNotFoundException("File does not exist: " + file);
        }
    }


    public static byte[] localFileToByteArray(File file) throws Throwable {
        return inputStreamToBa(new FileInputStream(file));
    }

    public static void byteArrayToLocalFile(byte[] fileAsBa, File localFile, long timestamp) throws Exception {
        BufferedOutputStream bs = new BufferedOutputStream(new FileOutputStream(localFile));
        bs.write(fileAsBa);
        bs.flush();
        bs.close();
        localFile.setLastModified(timestamp);
    }

    public static byte[] inputStreamToBa(InputStream is) throws Throwable {
        Throwable th;
        ByteArrayOutputStream baOs = null;
        BufferedInputStream bis = null;
        try {
            byte[] buff = new byte[BUFFER_SIZE];
            BufferedInputStream bis2 = new BufferedInputStream(is);
            try {
                ByteArrayOutputStream baOs2 = new ByteArrayOutputStream();
                while (true) {
                    try {
                        int count = bis2.read(buff);
                        if (count == -1) {
                            break;
                        }
                        baOs2.write(buff, 0, count);
                    } catch (Throwable th2) {
                        th = th2;
                        bis = bis2;
                        baOs = baOs2;
                    }
                }
                baOs2.flush();
                byte[] toByteArray = baOs2.toByteArray();
                if (baOs2 != null) {
                    try {
                        baOs2.close();
                    } catch (Exception e) {
                    }
                }
                if (bis2 != null) {
                    try {
                        bis2.close();
                    } catch (Exception e2) {
                    }
                }
                return toByteArray;
            } catch (Throwable th3) {
                th = th3;
                bis = bis2;
                if (baOs != null) {
                    try {
                        baOs.close();
                    } catch (Exception e3) {
                    }
                }
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (Exception e4) {
                    }
                }
                throw th;
            }
        } catch (Throwable th4) {
            th = th4;
            if (baOs != null) {
                baOs.close();
            }
            if (bis != null) {
                bis.close();
            }
            throw th;
        }
    }

    public static void copyFile(File sourceFile, File destFile) throws IOException {
        if (!destFile.exists()) {
            destFile.createNewFile();
        }
        FileChannel source = null;
        FileChannel destination = null;
        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }

}
